ALTER PROC sp_booking_summary_report
@PRODUCT_IDS VARCHAR(50),
@CUSTOMER_ID INT,
@CATEGORY VARCHAR(50),
@PERIODICAL VARCHAR(50),
@DATE_FROM  VARCHAR(50),
@DATE_T0  VARCHAR(50),
@CURRENT_QUARTER VARCHAR(4),
@CURRENT_YEAR VARCHAR(4),
@DATE_RANGE  VARCHAR(10), 
@DEFAULT_PROMPT VARCHAR(20)

AS

DECLARE @PROCESS_STEP_ID INT 
DECLARE @PROCESS_STEPS_TABLE TABLE( ProcessStep_ID INT )
DECLARE @TEMP_PROCESS_STEPS_TABLE TABLE( ProcessStep_ID INT )
DECLARE @PROCESS_STEPS_LOCATION TABLE( ProcessStep_ID INT, Location_ID INT )
DECLARE @TEMP_PRODUCT_ID INT
DECLARE @REQUESTED_BACKLOG_QTY INT
DECLARE @SCHEDULED_BACKLOG_QTY INT
DECLARE @REVISED_BACKLOG_QTY INT
DECLARE @TEMP_FROM_DATE DATETIME
DECLARE @TEMP_TO_DATE DATETIME
DECLARE @TEMP_PS_TYPE VARCHAR(20)
DECLARE @L_FROM_DATE  VARCHAR(20)
DECLARE @L_TO_DATE  VARCHAR(20)
DECLARE @SCH_DATE VARCHAR(50)
DECLARE @REQ_DATE VARCHAR(50)
DECLARE @REV_DATE VARCHAR(50)

BEGIN
	DECLARE @RESULT_BASEPRODUCTNAME VARCHAR(50)
	DECLARE @RESULT_BASE_PRODUCT_ID VARCHAR(20)
	DECLARE @RESULT_PROCESS_STEP_ID INT
	DECLARE @RESULT_PRODUCT_ID INT
	DECLARE @RESULT_PRODUCT VARCHAR(50)
	DECLARE @RESULT_PS_TYPE VARCHAR(50)
	DECLARE @RESULT_PRODUCT_TYPE VARCHAR(50)
	DECLARE @RESULT_PRODUCT_TYPE_ID INT
	DECLARE @RESULT_REQ_SCH_DATE DATETIME
	DECLARE @RESULT_SHIPPED_QTY BIGINT
	DECLARE @RESULT_UNIT_COST FLOAT
	DECLARE @RESULT_AMOUNT FLOAT


	DECLARE @REQUESTED_DATE DATETIME
	DECLARE @SCHEDULED_DATE DATETIME
	DECLARE @BOOKING_DATE DATETIME
	DECLARE @TEMP_SHIPPED_QTY BIGINT
	DECLARE @TEMP_UNIT_COST FLOAT
	

	DECLARE @SHIP_REQ_SCH_DT DATETIME 
	DECLARE @SHIP_YR_I INT
	DECLARE @SHIP_MT_I INT
	DECLARE @SHIP_QT_I INT
	DECLARE @SHIP_QT_END_I INT
	DECLARE @ACT_YEAR INT
	DECLARE @CHK_PRODUCT_FOR_BOARD_OR_KIT VARCHAR(50)
	DECLARE @SALES_MIN_REQ_DATE DATETIME
	DECLARE @SALES_MAX_REQ_DATE DATETIME
	
	DECLARE @SALES_MIN_SCH_DATE DATETIME
	DECLARE @SALES_MAX_SCH_DATE DATETIME

	DECLARE @SALES_FROM_DATE DATETIME
	DECLARE @SALES_TO_DATE DATETIME
	DECLARE @TEMP_DATE VARCHAR(20)	

	INSERT INTO @PROCESS_STEPS_TABLE( ProcessStep_ID ) SELECT ProcessStep_ID FROM FN_GET_PROCESS_STEPS_IN_STRING( @PRODUCT_IDS )

	DECLARE @RESULTS_TABLE TABLE ( BaseProductName VARCHAR(50),
					BaseProductID VARCHAR(20),
					ProcessStepID INT,
					ProductID INT,
					Product VARCHAR(50),
					PS_Type VARCHAR(50),
					Product_Type VARCHAR(50),
					Product_Type_ID INT,
					Req_Sch_Date DATETIME,
					Shipped_Qty INT, 
					Unit_Cost FLOAT,
					Amount FLOAT,
					ShipmentMonth INT, 
					ShipmentYear INT, 
					QuarterStart INT, 
					QuarterEnd INT, 
					ActYear INT)	


	DECLARE @SEARCH_RESULTS_TABLE TABLE ( BaseProductName VARCHAR(50),
					BaseProductID VARCHAR(20),
					ProcessStepID INT,
					ProductID INT,
					Product VARCHAR(50),
					PS_Type VARCHAR(50),
					Product_Type VARCHAR(50),
					Product_Type_ID INT,
					Req_Sch_Date DATETIME,
					Shipped_Qty INT, 
					Unit_Cost FLOAT,
					Amount FLOAT,
					ShipmentMonth INT, 
					ShipmentYear INT, 
					QuarterStart INT, 
					QuarterEnd INT, 
					ActYear INT)	
	
	DECLARE @LINEITEM_ALL_PRODUCTS_TABLE TABLE ( BaseProductName VARCHAR(50),
					BaseProductID VARCHAR(20),
					ProcessStepID INT,
					ProductID INT,
					Product VARCHAR(50),
					PS_Type VARCHAR(50),
					Product_Type VARCHAR(50),
					Product_Type_ID INT,
					Req_Sch_Date DATETIME,
					Shipped_Qty BIGINT, 
					Unit_Cost FLOAT,
					Amount FLOAT,
					ShipmentMonth INT, 
					ShipmentYear INT, 
					QuarterStart INT, 
					QuarterEnd INT, 
					ActYear INT)

	DECLARE @PARENT_PRODUCT_CURSOR CURSOR
	DECLARE @LINE_ITEM_CURSOR CURSOR


	SET @RESULT_BASEPRODUCTNAME  	= NULL	
	SET @RESULT_PROCESS_STEP_ID 	= NULL
	SET @RESULT_PRODUCT_ID 		= NULL
	SET @RESULT_PRODUCT  		= NULL

	IF @PRODUCT_IDS <> '0'
	BEGIN
		INSERT INTO @SEARCH_RESULTS_TABLE 
		SELECT 
		( 
		SELECT PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID 
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID	
		END 
		) AS BASEPRODUCTNAME, 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID
		END ,
		P.PROCESSSTEP_ID, P.PRODUCT_ID, PART_NUMBER, PS_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 'CHIP'
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 'BOARD'
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 'KIT'
		END AS PRODUCT_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 1
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
		END AS PRODUCT_TYPE_ID, NULL, 0, 0, 0, 0, 0, 0, 0, 0
		FROM PRODUCT P, PROCESS_STEP 
		WHERE P.PROCESSSTEP_ID = PROCESS_STEP.PROCESSSTEP_ID
		AND ( P_PARENT_PRODUCT_ID IN (SELECT ProcessStep_ID FROM @PROCESS_STEPS_TABLE) OR P.PRODUCT_ID = (SELECT ProcessStep_ID FROM @PROCESS_STEPS_TABLE) )
		AND P_PARENT_PRODUCT_ID IS NOT NULL 
		AND PS_TYPE_NAME = 'FINISHED GOODS' AND ( P_TYPE IS NULL OR P_TYPE = 'BOARD' OR P_TYPE = 'KIT')
		ORDER BY PRODUCT_TYPE_ID, BaseProductName 	
	END
	ELSE
	BEGIN
		INSERT INTO @SEARCH_RESULTS_TABLE 
		SELECT 
		( 
		SELECT PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID 
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID
		END
		) AS BASEPRODUCTNAME, 
		CASE 
			WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID
			WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID
		END ,
		P.PROCESSSTEP_ID, P.PRODUCT_ID, PART_NUMBER, PS_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 'CHIP'
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 'BOARD'
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 'KIT'
		END AS PRODUCT_TYPE,
		CASE 
			WHEN P_TYPE IS NULL THEN 1
			WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
			WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
		END AS PRODUCT_TYPE_ID, NULL, 0, 0, 0, 0, 0, 0, 0, 0
		FROM PRODUCT P, PROCESS_STEP 
		WHERE P.PROCESSSTEP_ID = PROCESS_STEP.PROCESSSTEP_ID
		AND P_PARENT_PRODUCT_ID IS NOT NULL 
		AND PS_TYPE_NAME = 'FINISHED GOODS' AND ( P_TYPE IS NULL OR P_TYPE = 'BOARD' OR P_TYPE = 'KIT')
		ORDER BY PRODUCT_TYPE_ID, BaseProductName 
	END

	INSERT INTO @LINEITEM_ALL_PRODUCTS_TABLE
	SELECT 
	( 
	SELECT PART_NUMBER FROM PRODUCT WHERE PRODUCT_ID = 
	CASE 
		WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID 
		WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID	
	END 
	) AS BASEPRODUCTNAME, 
	CASE 
		WHEN P.P_PARENT_PRODUCT_ID = -1 THEN P.PRODUCT_ID
		WHEN P.P_PARENT_PRODUCT_ID <> -1 THEN P.P_PARENT_PRODUCT_ID
	END ,
	P.PROCESSSTEP_ID, P.PRODUCT_ID, PART_NUMBER, PS_TYPE,
	CASE 
		WHEN P_TYPE IS NULL THEN 'CHIP'
		WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 'BOARD'
		WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 'KIT'
	END AS PRODUCT_TYPE,
	CASE 
		WHEN P_TYPE IS NULL THEN 1
		WHEN P_TYPE LIKE LTRIM(RTRIM('BOARD')) THEN 2
		WHEN P_TYPE LIKE LTRIM(RTRIM('KIT')) THEN 3
	END AS PRODUCT_TYPE_ID, NULL, 0, 0, 0, 0, 0, 0, 0, 0
	FROM PRODUCT P, PROCESS_STEP 
	WHERE P.PROCESSSTEP_ID = PROCESS_STEP.PROCESSSTEP_ID AND P.PRODUCT_ID IN (SELECT DISTINCT Product_ID FROM Customer_Open_Order_LineItem)

	IF @PRODUCT_IDS <> '0'
	BEGIN
		DELETE FROM @LINEITEM_ALL_PRODUCTS_TABLE WHERE BaseProductID <> @PRODUCT_IDS
	END

	INSERT INTO @SEARCH_RESULTS_TABLE SELECT * FROM @LINEITEM_ALL_PRODUCTS_TABLE WHERE ProductID NOT IN ( SELECT ProductID FROM @SEARCH_RESULTS_TABLE )

	SET @PARENT_PRODUCT_CURSOR = CURSOR FOR 
	SELECT BaseProductName, BaseProductID, ProcessStepID, ProductID, Product, PS_Type, Product_Type, Product_Type_ID FROM @SEARCH_RESULTS_TABLE
	ORDER BY Product_Type_ID, BaseProductName, ProcessStepID, ProductID


	OPEN @PARENT_PRODUCT_CURSOR 
	FETCH NEXT FROM @PARENT_PRODUCT_CURSOR INTO @RESULT_BASEPRODUCTNAME, @RESULT_BASE_PRODUCT_ID, @RESULT_PROCESS_STEP_ID, @RESULT_PRODUCT_ID, @RESULT_PRODUCT,
	@RESULT_PS_TYPE, @RESULT_PRODUCT_TYPE, @RESULT_PRODUCT_TYPE_ID
	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @REQUESTED_DATE = NULL
		SET @SCHEDULED_DATE = NULL
		SET @BOOKING_DATE   = NULL
		SET @TEMP_SHIPPED_QTY = NULL
		SET @TEMP_UNIT_COST = NULL

		SET @SHIP_REQ_SCH_DT = NULL
		SET @SHIP_YR_I = NULL
		SET @SHIP_MT_I = NULL
		SET @SHIP_QT_I = NULL
		SET @SHIP_QT_END_I = NULL
		SET @ACT_YEAR = NULL

		--PRINT '  @RESULT_PRODUCT_ID = ' + CONVERT(VARCHAR,@RESULT_PRODUCT_ID) + '  @RESULT_PRODUCT = ' + @RESULT_PRODUCT

		IF ( @CURRENT_QUARTER = 'ALL' )
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT COOL.COOL_Unit_Cost, COOL.COOL_Qty, SO.SO_Booking_Date,
			COOL.COOL_Requested_Date, ISNULL(COOL.COOL_Schedule_Date,'')
			FROM Customer_Open_Order_LineItem COOL, Sales_Order SO
			WHERE COOL.SalesOrder_ID = SO.SalesOrder_ID AND SO.SO_Is_Sample = 1 AND COOL.Product_ID = @RESULT_PRODUCT_ID  AND YEAR( SO.SO_Booking_Date ) = @CURRENT_YEAR

		END
		ELSE
		BEGIN
			SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT COOL.COOL_Unit_Cost, COOL.COOL_Qty, SO.SO_Booking_Date,
			COOL.COOL_Requested_Date, ISNULL(COOL.COOL_Schedule_Date,'')
			FROM Customer_Open_Order_LineItem COOL, Sales_Order SO
			WHERE COOL.SalesOrder_ID = SO.SalesOrder_ID AND SO.SO_Is_Sample = 1 AND COOL.Product_ID = @RESULT_PRODUCT_ID  
			AND DATEPART(Q, SO.SO_Booking_Date ) = @CURRENT_QUARTER AND YEAR( SO.SO_Booking_Date ) = @CURRENT_YEAR
		END

		/*SET @LINE_ITEM_CURSOR = CURSOR FOR SELECT COOL.COOL_Unit_Cost, COOL.COOL_Qty, SO.SO_Booking_Date,
		COOL.COOL_Requested_Date, ISNULL(COOL.COOL_Schedule_Date,'')
		FROM Customer_Open_Order_LineItem COOL, Sales_Order SO
		WHERE COOL.SalesOrder_ID = SO.SalesOrder_ID AND SO.SO_Is_Sample = 1 AND COOL.Product_ID = @RESULT_PRODUCT_ID 
		--GROUP BY SO.SO_Booking_Date, COOL.COOL_Requested_Date, COOL.COOL_Schedule_Date 
		*/
		OPEN @LINE_ITEM_CURSOR 
		FETCH NEXT FROM @LINE_ITEM_CURSOR INTO @TEMP_UNIT_COST, @TEMP_SHIPPED_QTY, @BOOKING_DATE, @REQUESTED_DATE, @SCHEDULED_DATE
		WHILE @@FETCH_STATUS = 0
		BEGIN
			/*SET @TEMP_DATE = NULL
			IF @CATEGORY = 'SCHEDULE' 
			BEGIN	
				--PRINT '@SCHEDULED_DATE = ' + CONVERT( VARCHAR, @SCHEDULED_DATE ,101)
				IF( @SCHEDULED_DATE IS NULL OR @SCHEDULED_DATE = '' OR CONVERT(VARCHAR,@SCHEDULED_DATE,101) = '01/01/1900')
				BEGIN
					SET @TEMP_DATE 		= CONVERT( VARCHAR, CURRENT_TIMESTAMP, 101)
					SET @SCHEDULED_DATE 	= CONVERT( DATETIME, @TEMP_DATE)
				END
				SET @SHIP_REQ_SCH_DT = @SCHEDULED_DATE
			END
			ELSE
			BEGIN
				SET @SHIP_REQ_SCH_DT = @REQUESTED_DATE
			END*/
			
			SET @SHIP_REQ_SCH_DT = @BOOKING_DATE

			SET @SHIP_YR_I = DATEPART(YYYY, @SHIP_REQ_SCH_DT)
			SET @SHIP_MT_I = DATEPART(MM, @SHIP_REQ_SCH_DT)
			SET @ACT_YEAR = @SHIP_YR_I 
			
			IF (@SHIP_MT_I >=1 AND @SHIP_MT_I <=3)
			BEGIN
				SET @SHIP_QT_I = 1
				SET @SHIP_QT_END_I = 3
			END
			ELSE IF (@SHIP_MT_I >=4 AND @SHIP_MT_I <=6)
			BEGIN
				SET @SHIP_QT_I = 2
				SET @SHIP_QT_END_I = 6 
			END
			ELSE IF (@SHIP_MT_I >=7 AND @SHIP_MT_I <=9)
			BEGIN
				SET @SHIP_QT_I = 3
				SET @SHIP_QT_END_I = 9
			END						
			ELSE IF (@SHIP_MT_I >=10 AND @SHIP_MT_I <=12)
			BEGIN
				SET @SHIP_QT_I = 4
				SET @SHIP_QT_END_I = 12
			END

			INSERT INTO @RESULTS_TABLE VALUES( @RESULT_BASEPRODUCTNAME, @RESULT_BASE_PRODUCT_ID, @RESULT_PROCESS_STEP_ID, @RESULT_PRODUCT_ID, @RESULT_PRODUCT,
			@RESULT_PS_TYPE, @RESULT_PRODUCT_TYPE, @RESULT_PRODUCT_TYPE_ID, @SHIP_REQ_SCH_DT, @TEMP_SHIPPED_QTY, @TEMP_UNIT_COST, 
			(  @TEMP_SHIPPED_QTY * @TEMP_UNIT_COST ), @SHIP_MT_I, @SHIP_YR_I, @SHIP_QT_I, @SHIP_QT_END_I, @SHIP_YR_I )

			SET @REQUESTED_DATE = NULL
			SET @SCHEDULED_DATE = NULL
			SET @BOOKING_DATE   = NULL
			SET @TEMP_SHIPPED_QTY = NULL
			SET @TEMP_UNIT_COST = NULL
	
			SET @SHIP_REQ_SCH_DT = NULL
			SET @SHIP_YR_I = NULL
			SET @SHIP_MT_I = NULL
			SET @SHIP_QT_I = NULL
			SET @SHIP_QT_END_I = NULL
			SET @ACT_YEAR = NULL

			SET @TEMP_UNIT_COST = NULL
			SET @TEMP_SHIPPED_QTY = NULL
			SET @BOOKING_DATE = NULL
			SET @REQUESTED_DATE = NULL
			SET @SCHEDULED_DATE = NULL
			SET @TEMP_DATE = NULL
			
			
			FETCH NEXT FROM @LINE_ITEM_CURSOR INTO @TEMP_UNIT_COST, @TEMP_SHIPPED_QTY, @BOOKING_DATE, @REQUESTED_DATE, @SCHEDULED_DATE
		END
		CLOSE @LINE_ITEM_CURSOR
		DEALLOCATE @LINE_ITEM_CURSOR
		
		SET @RESULT_BASEPRODUCTNAME = NULL
		SET @RESULT_BASE_PRODUCT_ID = NULL
		SET @RESULT_PROCESS_STEP_ID = NULL
		SET @RESULT_PRODUCT_ID = NULL
		SET @RESULT_PRODUCT = NULL
		SET @RESULT_PS_TYPE = NULL
		SET @RESULT_PRODUCT_TYPE = NULL
		SET @RESULT_PRODUCT_TYPE_ID = NULL

		FETCH NEXT FROM @PARENT_PRODUCT_CURSOR INTO @RESULT_BASEPRODUCTNAME, @RESULT_BASE_PRODUCT_ID, @RESULT_PROCESS_STEP_ID, @RESULT_PRODUCT_ID, @RESULT_PRODUCT,
		@RESULT_PS_TYPE, @RESULT_PRODUCT_TYPE, @RESULT_PRODUCT_TYPE_ID
	END
	CLOSE @PARENT_PRODUCT_CURSOR
	DEALLOCATE @PARENT_PRODUCT_CURSOR

	SET @SHIP_REQ_SCH_DT = NULL
	SET @SHIP_YR_I = NULL
	SET @SHIP_MT_I = NULL
	SET @SHIP_QT_I = NULL
	SET @SHIP_QT_END_I = NULL
	SET @ACT_YEAR = NULL
	SET @SHIP_REQ_SCH_DT = NULL

	SET @SHIP_YR_I = @CURRENT_YEAR
	SET @ACT_YEAR = @CURRENT_YEAR 

	IF ( @CURRENT_QUARTER = 'ALL' OR @CURRENT_QUARTER = '1' )
	BEGIN
		SET @SHIP_MT_I = 1
		SET @SHIP_QT_I = 1
		SET @SHIP_QT_END_I = 3
		SET @SHIP_REQ_SCH_DT = CONVERT( DATETIME, '01/01/' + CONVERT(VARCHAR,@SHIP_YR_I) )
	END
	ELSE
	BEGIN
		IF ( @CURRENT_QUARTER = '2' )
		BEGIN
			SET @SHIP_MT_I = 4
			SET @SHIP_QT_I = 2
			SET @SHIP_QT_END_I = 6
			SET @SHIP_REQ_SCH_DT = CONVERT( DATETIME, '04/01/' +  CONVERT(VARCHAR,@SHIP_YR_I) )
		END
		ELSE IF ( @CURRENT_QUARTER = '3' )
		BEGIN
			SET @SHIP_MT_I = 7
			SET @SHIP_QT_I = 3
			SET @SHIP_QT_END_I = 9
			SET @SHIP_REQ_SCH_DT = CONVERT( DATETIME, '07/01/' +  CONVERT(VARCHAR,@SHIP_YR_I) )
		END
		ELSE IF ( @CURRENT_QUARTER = '4' )
		BEGIN
			SET @SHIP_MT_I = 10
			SET @SHIP_QT_I = 4
			SET @SHIP_QT_END_I = 12
			SET @SHIP_REQ_SCH_DT = CONVERT( DATETIME, '10/01/' +  CONVERT(VARCHAR,@SHIP_YR_I) )
		END
	END

	DECLARE @RESULTS_TABLE_COUNT INT	
	SELECT @RESULTS_TABLE_COUNT = COUNT(*) FROM @RESULTS_TABLE

	IF @RESULTS_TABLE_COUNT > 0
	BEGIN
		DELETE FROM @SEARCH_RESULTS_TABLE WHERE ProductID IN ( SELECT ProductID FROM @RESULTS_TABLE )

		UPDATE @SEARCH_RESULTS_TABLE SET Req_Sch_Date = @SHIP_REQ_SCH_DT, Shipped_Qty = 0, Unit_Cost = 0, Amount = 0,
		ShipmentMonth = @SHIP_MT_I,  ShipmentYear = @CURRENT_YEAR,  QuarterStart = @SHIP_QT_I,  QuarterEnd = @SHIP_QT_END_I,  ActYear = @CURRENT_YEAR
	
		INSERT INTO @SEARCH_RESULTS_TABLE SELECT * FROM @RESULTS_TABLE
	END
	ELSE
	BEGIN
		UPDATE @SEARCH_RESULTS_TABLE SET Req_Sch_Date = @SHIP_REQ_SCH_DT, Shipped_Qty = 0, Unit_Cost = 0, Amount = 0,
		ShipmentMonth = @SHIP_MT_I,  ShipmentYear = @CURRENT_YEAR,  QuarterStart = @SHIP_QT_I,  QuarterEnd = @SHIP_QT_END_I,  ActYear = @CURRENT_YEAR
	END

	--SELECT * FROM @RESULTS_TABLE WHERE CONVERT(VARCHAR,Req_Sch_Date,101) BETWEEN @SALES_FROM_DATE AND @SALES_TO_DATE ORDER BY Product_Type_ID, BaseProductName, Product 
	--SELECT * FROM @RESULTS_TABLE --WHERE CONVERT(VARCHAR,Req_Sch_Date,101) BETWEEN @SALES_FROM_DATE AND @SALES_TO_DATE ORDER BY Product_Type_ID, BaseProductName, Product 
	--SELECT * FROM @RESULTS_TABLE WHERE DATEPART(Q, Req_Sch_Date ) = @CURRENT_QUARTER AND YEAR( Req_Sch_Date ) = @CURRENT_YEAR ORDER BY Product_Type_ID, BaseProductName, Product 
	IF @CURRENT_QUARTER = 'ALL'
	BEGIN
		SELECT * FROM @SEARCH_RESULTS_TABLE WHERE YEAR( Req_Sch_Date ) = @CURRENT_YEAR ORDER BY Product_Type_ID, BaseProductName, Product 
	END
	ELSE
	BEGIN
		SELECT * FROM @SEARCH_RESULTS_TABLE WHERE DATEPART(Q, Req_Sch_Date ) = @CURRENT_QUARTER  AND YEAR( Req_Sch_Date ) = @CURRENT_YEAR ORDER BY Product_Type_ID, BaseProductName, Product 
	END
END
